package com.mall.b2c.template.directive;

import static com.mall.common.directive.DirectiveUtils.OUT_LIST;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.mall.b2c.bean.ArticleBean;
import com.mall.b2c.hessian.CacheService;
import com.mall.common.directive.DirectiveUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * Created by lzx on 2015/5/5.
 */
@Repository
public class ArticleListDirective extends BaseDirective {

    @Resource
    private CacheService cacheService;
    /**
     * 输入参数，栏目ID。
     */
    public static final String PARAM_CHANNEL_ID = "categoryId";
    public static final String PARAM_ARTICLE_PU = "isPublication";
    public static final String PARAM_ARTICLE_TOP = "isTop";
    public static final String PARAM_ARTICLE_RECOMMEND = "isRecommend";
    public static final String PARAM_ARTICLE_ORDERBY = "orderBy";
    public static final String PARAM_NUM = "num";
    public static final String PARAM_CHANNEL_CODE = "categoryCode";


    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody body) throws TemplateException, IOException {
        List<ArticleBean> list= getList(params, env);
        Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(params);
        paramWrap.put(OUT_LIST, DEFAULT_WRAPPER.wrap(list));
        Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(env, paramWrap);
        body.render(env.getOut());
        DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);

    }

    private Long getCategoryId(Map<String, TemplateModel> params)	throws TemplateException {
        Long categoryId = DirectiveUtils.getLong(PARAM_CHANNEL_ID, params);
        if (categoryId != null) {
            return categoryId;
        } else {
            return null;
        }
    }

    private String getOrderBy(Map<String, TemplateModel> params)	throws TemplateException {
        String orderBy = DirectiveUtils.getString(PARAM_ARTICLE_ORDERBY, params);
        if (orderBy != null) {
            return orderBy;
        } else {
            return null;
        }
    }

    private Boolean getIsPub(Map<String, TemplateModel> params)	throws TemplateException {
        Boolean isPublication = DirectiveUtils.getBool(PARAM_ARTICLE_PU, params);
        if (isPublication != null) {
            return isPublication;
        } else {
            return null;
        }
    }

    private Boolean getIsRecommend(Map<String, TemplateModel> params) throws TemplateException {
        Boolean isRecommend = DirectiveUtils.getBool(PARAM_ARTICLE_RECOMMEND, params);
        if (isRecommend != null) {
            return isRecommend;
        } else {
            return null;
        }
    }

    private Integer getNum(Map<String, TemplateModel> params) throws TemplateException {
        String num = DirectiveUtils.getString(PARAM_NUM, params);
        if(StringUtils.isEmpty(num)){
            return null;
        }else{
            return Integer.valueOf(num);
        }
    }

    private Boolean getIsTop(Map<String, TemplateModel> params) throws TemplateException {
        Boolean isTop = DirectiveUtils.getBool(PARAM_ARTICLE_TOP, params);
        if (isTop != null) {
            return isTop;
        } else {
            return null;
        }
    }

    private List<ArticleBean> getList(Map<String, TemplateModel> params,
                                      Environment env) throws TemplateException {
        Long categoryId = getCategoryId(params);
        Boolean isPublication = getIsPub(params);
        Boolean isRecommend = getIsRecommend(params);
        Boolean isTop = getIsTop(params);
        String orderBy = getOrderBy(params);
        Integer num = getNum(params);
        String categoryCode = DirectiveUtils.getString(PARAM_CHANNEL_CODE, params);

        return cacheService.getAticles(categoryId, isPublication, isRecommend, isTop, orderBy, num, categoryCode);
    }
}
